package limiter

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"golang.org/x/time/rate"
	"ydd-im/util/response"
	"time"
)

func SetUp (maxBurstSize int) gin.HandlerFunc { // 这个数字要大于连接数，否者出现错误拦截

	limiter := rate.NewLimiter(rate.Every(time.Second*1), maxBurstSize)
	return func(c *gin.Context) {
		if limiter.Allow() {
			c.Next()
			return
		}
		fmt.Println("Too many requests")
		response.ResponseJsonFail(c,response.Response{
			Code:response.ErrorCode,
			Message:"Too many requests",
		})
		c.Abort()
		return
	}
}
